package com.cn.studySpring.controller;

import com.cn.studySpring.dto.Message;
import com.cn.studySpring.pojo.Log;
import com.cn.studySpring.pojo.OnlineUser;
import com.cn.studySpring.pojo.User;
import com.cn.studySpring.service.LogService;
import com.cn.studySpring.service.UserService;
import com.cn.studySpring.util.*;
import net.sf.json.JSONObject;
import org.apache.ibatis.io.ResolverUtil.Test;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import sun.misc.BASE64Decoder;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class AccountController {

	@Value("${qiniu.domain}")
	private String domain;

	UserService userService = (UserService) SpringBeanUtil.getBean("userService");
	private static Logger log = Logger.getLogger(Test.class.getClass());

	 //返回的是一个包含模型和视图的ModelAndView对象；
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public ModelAndView show(Integer id) throws AuthenticationException {

		//userService.addUser();
		/*userService.updateUser();
		userService.deleteUser();*/
		String MD5Domin = MD5Util.getMD5(domain);
		String MD5Domin2 = MD5Util.MD5(domain);
		System.out.println(MD5Domin);
		System.out.println(MD5Domin2);
		boolean s = MD5Domin.equals(MD5Domin2);

		System.err.println(s);
		if (s == false) {
			//throw new AuthenticationException("sadasd");
		}
		ModelAndView mo = new ModelAndView();
		mo.setViewName("hello");
		return mo;
	}
	
    @RequestMapping(value="/hello")

    public Map<String, String> index3(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "value-1"); 
        map.put("key2", "value-2");
        return map;
      //map.put相当于request.setAttribute方法
      //响应的view应该也是该请求的view。等同于void返回。return 'hello'
       
    }
    
    
    //返回String

    //通过model进行使用

    @RequestMapping(value="/index4",method = RequestMethod.GET)
  
    //RedirectAttributes重定向之后还能带参数跳转的的工具类
    public String index(Model model,RedirectAttributes redirectAttributes) {

    	
        String retVal = "hello";

        OnlineUser onlineUser = null;

        onlineUser.setNickname("asdqsda");

        model.addAttribute("user", onlineUser);
        redirectAttributes.addFlashAttribute("message",new Message(Message.ERROR,"账号或密码错误"));
        redirectAttributes.addFlashAttribute("message2","message21231");
        return "redirect:/hello";

    }
    
    @RequestMapping("/index5")
    public Object index5(@ModelAttribute("message") String error){
          System.out.println("登录出错了:"+error);
		return error;
    }
    @RequestMapping("/index6")
    public String index5(RedirectAttributes redirectAttributes){
    	 redirectAttributes.addFlashAttribute("message",new Message(Message.ERROR,"账号或密码错误"));
         redirectAttributes.addFlashAttribute("message2","message21231");
         return "redirect:/hello";
    }

	//自动装箱
	@ResponseBody
	@RequestMapping(value = "/show", method = RequestMethod.POST)
	public OnlineUser addUser(OnlineUser onlineUser) throws AuthenticationException {
		OnlineUser onlineUser1 = onlineUser;
		
		return onlineUser1;//直接转成json
	}

}
